<?php
declare (strict_types = 1);
/**
 * 管理员
 */
namespace app\common\admin;

use app\common\facade\model\AdminModel;
use app\common\facade\model\AdminRoleModel;
use app\common\facade\model\AdminRuleModel;

class Admin extends BaseAdmin
{

    /**
     * 管理员列表
     */
    public function adminList($param = [])
    {

        $whereAdmin = [];

        if (isset($param['name']) && !empty($param['name'])) $whereAdmin[] = ['name', 'like', "%{$param['name']}%"];

        $adminList = AdminModel::adminList($whereAdmin, [], $param['offset'], $param['length']);

        return $adminList;
    }

    /**
     * 角色【所有】
     */
    public function roleAll($param = [], $field = '*')
    {

        $where = [];

        if (isset($param['enable'])) $where[] = ['enable', '=', $param['enable']];

        return AdminRoleModel::listsAll($where, $field);
    }

    /**
     * 权限【管理员、所有】
     */
    public function ruleAdminAll()
    {

        $adminLoginInfo = $this->adminLoginInfo();

        if ($adminLoginInfo['roleId'] != 0):

            $whereRole = [
                ['id', '=', $adminLoginInfo['roleId']],
                ['enable', '=', 2]
            ];

            $infoRole = AdminRoleModel::info($whereRole);

            if (empty($infoRole)) exception('没有权限访问!');

            $ruleId = explode(',', $infoRole['ruleId']);

            $where[] = ['id', 'in', $ruleId];
        endif;

        $where[] = ['enable', '=', 2];

        $listsRule = AdminRuleModel::listsAll($where);

        return $listsRule;
    }

    /**
     * 管理员添加编辑【保存】
     */
    public function adminAddEditSave($param = [])
    {

        $where = [];

        if (isset($param['password'])):

            if (empty($param['password'])):
                unset($param['password']);
            else:
                $param['password'] = password_hash($param['password'], PASSWORD_DEFAULT);
            endif;

            unset($param['password2']);
        endif;

        if (isset($param['id'])):
            $where[] = ['id', '=', $param['id']];
            unset($param['id']);
        endif;

        AdminModel::addEditSave($param, $where);

        return true;
    }

    /**
     * 修改密码【保存】
     */
    public function passwordEditSave($param = [])
    {

        $adminLoginInfo = self::adminLoginInfo();

        $param = [
            'id' => $adminLoginInfo['id']
        ];

        self::adminAddEditSave($param);

        return true;
    }

    /**
     * 管理员详情
     */
    public function adminInfo($param = [])
    {

        if (isset($param['name'])) $where[] = ['name', '=', $param['name']];

        if (isset($param['accessLogin'])) $where[] = ['accessLogin', '=', $param['accessLogin']];

        if (!isset($where)) exception();

        return AdminModel::info($where);
    }

    /**
     * 角色详情
     */
    public function roleInfo($param = [])
    {

        if (isset($param['roleId'])) $where[] = ['id', '=', $param['roleId']];

        if (!isset($where)) exception();

        $info = AdminRoleModel::info($where);

        if (isset($info['ruleId']) && !empty($info['ruleId'])) $info['ruleId'] = explode(',', $info['ruleId']);

        return $info;
    }

    /**
     * 权限详情
     */
    public function ruleInfo($param = [])
    {

        if (isset($param['ruleId'])) $where[] = ['id', 'in', $param['ruleId']];

        if (isset($param['path'])) $where[] = ['path', '=', $param['path']];

        if (!isset($where)) exception();

        return AdminRuleModel::info($where);
    }

}
